package com.corpgovernment.common.base;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import java.util.Map;
import java.util.concurrent.Callable;

/**
 */
public abstract class BaseCallable<V> implements Callable<V> {
	private static final Logger LOGGER = LoggerFactory.getLogger(BaseCallable.class);

	private Map<String, String> context;

	public BaseCallable(String type, String name) {
		context = MDC.getCopyOfContextMap();
	}

	/* (non-Javadoc)
	 * @see java.util.concurrent.Callable#call()
	 */
	@Override
	public final V call() throws Exception {
		if (context != null) {
			MDC.setContextMap(context);
		}
		V result = null;
		try {
			result = execute();
		} catch (Exception e) {
			LOGGER.error("异常", e);
			throw e;
		} finally {
			MDC.clear();
		}
		return result;
	}

	/**
	 * 执行方法
	 * 
	 * @return
	 */
	public abstract V execute();
}